package com.briup.smartcity_group7.service.impl;


import com.briup.smartcity_group7.bean.basic.BaseConfig;
import com.briup.smartcity_group7.dao.basic.BaseConfigMapper;
import com.briup.smartcity_group7.dao.ext.BaseConfigExtMapper;
import com.briup.smartcity_group7.exception.ServiceException;
import com.briup.smartcity_group7.service.IBaseConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class BaseConfigServiceImp implements IBaseConfigService {
    @Autowired
    private BaseConfigMapper mapper;
    @Autowired
    private BaseConfigExtMapper extendMapper;

    @Override
    public void saveConfig(BaseConfig baseConfig) throws ServiceException {
            mapper.insert(baseConfig);

    }

    @Override
    public void updateConfig(BaseConfig baseConfig) throws ServiceException {
        Integer id =baseConfig.getConfigId();
        BaseConfig baseConfig1 = mapper.selectByPrimaryKey(id);
        if (baseConfig1==null)
        {
            throw new ServiceException("id不存在");
        }
        mapper.updateByPrimaryKeySelective(baseConfig);


    }

    @Override
    public void changeConfigStatus(int id, int status) throws ServiceException {
        List<BaseConfig> baseConfigss = mapper.fingdAllByPageHeple();
            if (status == 1) {
                List<BaseConfig> baseConfigs = extendMapper.findAllStatusYes();
                if (baseConfigs.size() == 1) {
                    throw new ServiceException("无法更新,有其他配置处于启动状态");
                }

            }
        BaseConfig baseConfig = mapper.selectByPrimaryKey(id);
        if (baseConfig==null)
        {
            throw new ServiceException("id不存在");
        }
        mapper.upcongficstatus(id,status);

    }

    @Override
    public int deleteById(int id)  throws ServiceException {


        int i = mapper.deleteByPrimaryKey(id);
        if ( i==0 ){

            throw new ServiceException("id不存在");
        }
        return i;

    }

    @Override
    public PageInfo<BaseConfig> findAllWithPage(int pageNum, int pageSize) throws ServiceException {


        PageHelper.startPage(pageNum,pageSize,true);

        List<BaseConfig> configs = mapper.fingdAllByPageHeple();

        PageInfo<BaseConfig> pageInfo = new PageInfo<>(configs);
//        PageInfo<BaseConfig> pageInfo = PageHelper.startPage(pageNum, pageSize, true).doSelectPageInfo(
//                () -> mapper.fingdAllByPageHeple()
//        );

        return pageInfo;
    }

    @Override
    public BaseConfig currentConfigYes() throws ServiceException{
        BaseConfig baseConfig = mapper.YesBaseConfig();

        return baseConfig;

    }
}
